package com.suedawn.bookmanagesystem.utils;

import java.sql.*;

public class DBConnection {
    // 使用这个完整的连接字符串
    private static final String URL = "jdbc:mysql://localhost:3306/library_management_system?" +
            "useSSL=false&" +
            "allowPublicKeyRetrieval=true&" +
            "serverTimezone=UTC&" +
            "autoReconnect=true&" +
            "characterEncoding=UTF-8";

    private static final String USER = "root";
    private static final String PASSWORD = "123456"; // 替换为您的MySQL密码

    static {
        try {
            // 明确加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("MySQL JDBC驱动加载失败", e);
        }
    }

    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(URL, USER, PASSWORD);
    }

    public static void closeResources(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void closeResources(Connection conn, Statement stmt) {
        closeResources(conn, stmt, null);
    }
}




